# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=kicad
pkgbase=mingw-w64-${_realname}-git
_sub=("footprints" "symbols" "templates" "packages3D")
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=r15133.23086decc
pkgrel=1
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=("any")
url="http://www.kicad-pcb.org"
license=("GPLv3+")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
    "${MINGW_PACKAGE_PREFIX}-curl"
    "${MINGW_PACKAGE_PREFIX}-glew"
    "${MINGW_PACKAGE_PREFIX}-glm"
    "${MINGW_PACKAGE_PREFIX}-ngspice"
    "${MINGW_PACKAGE_PREFIX}-python2"
    "${MINGW_PACKAGE_PREFIX}-wxPython"
    "${MINGW_PACKAGE_PREFIX}-wxWidgets"
    "${MINGW_PACKAGE_PREFIX}-openssl"
    "${MINGW_PACKAGE_PREFIX}-freeglut"
    "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("git"
    "${MINGW_PACKAGE_PREFIX}-cmake"
    "${MINGW_PACKAGE_PREFIX}-gcc"
    "${MINGW_PACKAGE_PREFIX}-doxygen"
    "${MINGW_PACKAGE_PREFIX}-pkg-config"
    "${MINGW_PACKAGE_PREFIX}-swig"
    "${MINGW_PACKAGE_PREFIX}-cairo"
    "${MINGW_PACKAGE_PREFIX}-oce")

source=("${_realname}"::"git+https://github.com/KiCad/kicad-source-mirror.git"
    "${_realname}-i18n"::"git+https://github.com/KiCad/kicad-i18n.git"
    "glm_version_ok.patch")
sha256sums=('SKIP'
            'SKIP'
            'dddcfb6b10903b1b3886b7ff26a3a323b62fbfe2a00b7e523ae78b1920da4cda')

pkgver() {
  cd "${srcdir}/${_realname}"
  ( set -o pipefail
#    git describe --long 2>/dev/null | sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
  )
}

prepare() {
#  cp -f ${srcdir}/${_realname}/template/kicad.pro ${srcdir}/kicad-templates/
  
  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  [[ -d build-i18n ]] && rm -r build-i18n
  true
  
  # glm_version 9.9.3 is Ok
  patch -p0 -i ${srcdir}/glm_version_ok.patch
}

build_kicad() {
  cd "${srcdir}"
  # Configure and build KiCad.
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DCMAKE_PREFIX_PATH=;DEFAULT_INSTALL_PATH=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_btype} \
    -DDEFAULT_INSTALL_PATH=${MINGW_PREFIX} \
    -DOPENSSL_ROOT_DIR=${MINGW_PREFIX} \
    -DKICAD_SCRIPTING=ON \
    -DKICAD_SCRIPTING_MODULES=ON \
    -DKICAD_SCRIPTING_WXPYTHON=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python2.exe \
    ../${_realname}

  make

  cd "${srcdir}"
  # Configure the translationn installation build.
  mkdir build-i18n && cd build-i18n
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-i18n
}

_doc=("ca" "de" "en" "es" "fr" "id" "it" "ja" "nl" "pl" "ru" "zh")
for _d in ${_doc[@]}; do _doc_pkg+=("\"${MINGW_PACKAGE_PREFIX}-${_realname}-doc-${_d}: Documentation for KiCad (mingw-w64)\""); done
for _s in ${_sub[@]}; do _sub_pkg+=("${MINGW_PACKAGE_PREFIX}-${_realname}-${_s}-git"); done

eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-git() {
  optdepends+=("${_doc_pkg[@]}" "\${_sub_pkg[@]}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  
  build_kicad
  
  cd "\"\${srcdir}\"/build-${MINGW_CHOST}"
  make DESTDIR="\"\${pkgdir}\"" install

  cd "\"\${srcdir}\"/build-i18n"
  make DESTDIR="\"\${pkgdir}\"" install
}"
